home *** CD-ROM | disk | FTP | other *** search
/ Czech Logic, Card & Gambling Games / Logické hry.iso / hry / Robocode / robocode-setup-1.0.7.jar / extract.jar / robots / sample / Corners.java < prev    next >
Encoding:
Java Source  |  2005-02-18  |  3.4 KB  |  126 lines

  1. package sample;
  2. import robocode.*;
  3. /**
  4.  * Corners - a sample robot by Mathew Nelson
  5.  * 
  6.  * This robot moves to a corner, then swings the gun back and forth.
  7.  * If it dies, it tries a new corner in the next round.
  8.  */
  9. public class Corners extends Robot
  10. {
  11.     int others;                            // Number of other robots in the game
  12.     static int corner = 0;                // Which corner we are currently using
  13.                                         // static so that it keeps it between
  14.                                         // rounds.
  15.     boolean stopWhenSeeRobot = false;     // See goCorner()
  16.     
  17.     /**
  18.      * run: Corners' main run function.
  19.      */
  20.     public void run() {
  21.         // Save # of other bots
  22.         others = getOthers();
  23.         
  24.         // Move to a corner
  25.         goCorner();
  26.         
  27.         // Initialize gun turn speed to 3
  28.         int gunIncrement = 3;
  29.         
  30.         // Spin gun back and forth
  31.         while (true) {
  32.             for (int i = 0; i < 30; i++) {
  33.                 turnGunLeft(gunIncrement);
  34.             }
  35.             gunIncrement *= -1;
  36.         }
  37.     }
  38.     
  39.     /**
  40.      * goCorner: A very inefficient way to get to a corner.  Can you do better?
  41.      */
  42.     public void goCorner() {
  43.         // We don't want to stop when we're just turning...
  44.         stopWhenSeeRobot = false;
  45.         // turn to face the wall to the "right" of our desired corner.
  46.         turnRight(normalRelativeAngle(corner - getHeading()));
  47.         // Ok, now we don't want to crash into any robot in our way...
  48.         stopWhenSeeRobot = true;
  49.         // Move to that wall
  50.         ahead(5000);
  51.         // Turn to face the corner
  52.         turnLeft(90);
  53.         // Move to the corner
  54.         ahead(5000);
  55.         // Turn gun to starting point
  56.         turnGunLeft(90);
  57.     }
  58.     
  59.     /**
  60.      * onScannedRobot:  Stop and fire!
  61.      */
  62.     public void onScannedRobot(ScannedRobotEvent e) {
  63.         // Should we stop, or just fire?
  64.         if (stopWhenSeeRobot)
  65.         {
  66.             // Stop everything!  You can safely call stop multiple times.
  67.             stop();
  68.             // Call our custom firing method
  69.             smartFire(e.getDistance());
  70.             // Look for another robot.
  71.             // NOTE:  If you call scan() inside onScannedRobot, and it sees a robot,
  72.             // the game will interrupt the event handler and start it over
  73.             scan();
  74.             // We won't get here if we saw another robot.
  75.             // Okay, we didn't see another robot... start moving or turning again.
  76.             resume();
  77.         }
  78.         else
  79.             smartFire(e.getDistance());
  80.     }
  81.     
  82.     /**
  83.      * smartFire:  Custom fire method that determines firepower based on distance.
  84.      */
  85.     public void smartFire(double robotDistance) {
  86.         if (robotDistance > 200 || getEnergy() < 15)
  87.             fire(1);
  88.         else if (robotDistance > 50)
  89.             fire(2);
  90.         else
  91.             fire(3);
  92.     }
  93.  
  94.     /**
  95.      * onDeath:  We died.  Decide whether to try a different corner next game.
  96.      */
  97.      public void onDeath(DeathEvent e) {
  98.         // Well, others should never be 0, but better safe than sorry.
  99.         if (others == 0)
  100.             return;
  101.         
  102.         // If 75% of the robots are still alive when we die, we'll switch corners.
  103.         if ((others - getOthers()) / (double)others < .75) {
  104.             corner += 90;
  105.             if (corner == 270)
  106.                 corner = -90;
  107.             System.out.println("I died and did poorly... switching corner to " + corner);
  108.         }
  109.         else
  110.             System.out.println("I died but did well.  I will still use corner " + corner);
  111.     }
  112.     
  113.     /**
  114.      * normalRelativeAngle:  returns angle such that -180<angle<=180
  115.      */
  116.     public double normalRelativeAngle(double angle) {
  117.         if (angle > -180 && angle <= 180)
  118.             return angle;
  119.         double fixedAngle = angle;
  120.         while (fixedAngle <= -180)
  121.             fixedAngle += 360;
  122.         while (fixedAngle > 180)
  123.             fixedAngle -= 360;
  124.         return fixedAngle;
  125.     }
  126. }